package utilcollection;

import java.util.ArrayList;
import java.util.Spliterator;

public class SpliteratorDemo {
    public static void main(String[] args) {
        //Create an array list for doubles;
        ArrayList<Double> vals=new ArrayList<>();

        //Add values to the array list.
        vals.add(1.0);
        vals.add(2.0);
        vals.add(3.0);
        vals.add(4.0);
        vals.add(5.0);

        //Use tryAdvance() to display contents of vals.
        System.out.println("Contents of vals:");
        Spliterator<Double> spliterator=vals.spliterator();

        while (spliterator.tryAdvance((n)->System.out.println(n))) {
            ;
        }
        System.out.println();

        //Create new list thar contains square roots.
        spliterator=vals.spliterator();
        ArrayList<Double> sqrs=new ArrayList<>();
        while(spliterator.tryAdvance((n)->sqrs.add(Math.sqrt(n)))) {
            ;
        }

        //Use forEachRemaining() to display contents of sqrs;
        System.out.println("Contents of sqrs:");
        spliterator=sqrs.spliterator();
        spliterator.forEachRemaining((n)-> System.out.println(n));
        System.out.println();



    }
}